#include "table.h"

using namespace std;

void Table::insertRow(Row r) {
	rows.push_back(r);
}

void Table::removeRow(int i) {
	rows.erase(rows.begin()+i);
}

void Table::deleteWhere(vector<string> attributes, vector<string> values) {

	// Find all cases where a given attribute matches a given value and remove the corresponding row.
	// Supports multiple attributes and multiple values.
	// Example input: attributes [make, make, model] & values [ford, chevrolet, civic]
	for (int k=0; k<attributes.size(); k++) {
		for (int i=0; i<colTitles.size(); i++) {
			if (colTitles[i] == attributes[k]) {
				for (int j=0; j<rows.size(); j++) {
					if(rows[j][i].getData() == values[k]) {
						rows.erase(rows.begin()+j);
						//Currently deletes entry each time make = ford or model = mustang
						//Need to find a way to check both conditions at once
            //This is a test of Google Code
					}
        // Hello
				}
			}
		}
	}
}

Row Table::getRow(int i) {
	return rows[i];
}


Table Table::selectWhere(string title, vector<string> attributes, vector<string> values) {

	// Find all cases where a given attribute matches a given value and copy the corresponding row to a new table.
	// Supports multiple attributes and multiple values.
	// Example input: attributes [make, make, model] & values [ford, chevrolet, civic]
	Table *tempTable = new Table(title, attributes, startingKey);
	for (int k=0; k<attributes.size(); k++) {		//possibly switch with i loop
		for (int i=0; i<colTitles.size(); i++) { 	//possibly switch with k loop
			if (colTitles[i] == attributes[k]) {
				for (int j=0; j<rows.size(); j++) {
					if(rows[j][i].getData() == values[k]) { 
						tempTable->rows.push_back(rows[j]);
						//Currently creating an entry each time make = ford or model = mustang
						//Need to find a way to check both conditions at once
					}
				}
			}
		}
	}
	return *tempTable;


}

void Table::updateWhere(vector<string> attributes, vector<string> values, string updateValue) {

	// Find all cases where a given attribute matches a given value and update that case.
	// Supports multiple attributes and multiple values.
	// Example input: attributes [make, model] & values [chevrolet, mustang] & updateValue = ford
	for (int k=0; k<attributes.size(); k++) {
		for (int i=0; i<colTitles.size(); i++) {
			if (colTitles[i] == attributes[k]) {
				for (int j=0; j<rows.size(); j++) {
					if(rows[j][i].getData() == values[k]) { 
						//Currently creating an entry each time make = ford or model = mustang
						//Need to find a way to check both conditions at once
					}
				}
			}
		}
	}
}

Table Table::projection(string title, Table table, vector<int> colIndexes) {

	// Create a table that contains the values that match only the selected columns.
	vector<string> tempTitles;
	for (int i=0; i<colTitles.size(); i++) {
		for (int j=0; j<colIndexes.size(); j++) {
			if (i == colIndexes[j]) {
				tempTitles.push_back(colTitles[i]);
			}
		}
	}
	Table *tempTable = new Table(title, tempTitles, startingKey);
	for (int k=0; k<table.rows.size(); k++) {
		tempTable->insertRow(table.rows[k].partialRow(colIndexes));
	}
	return *tempTable;
}
